package com.mobartisan.workinglunch.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobartisan.workinglunch.R;
import com.mobartisan.workinglunch.SnakeApplication;
import com.mobartisan.workinglunch.constants.Constants;
import com.mobartisan.workinglunch.ui.base.BaseActivity;
import com.mobartisan.workinglunch.ui.fragment.RecordFragment;
import com.mobartisan.workinglunch.util.SharedPreferencesUtil;
import com.mobartisan.workinglunch.util.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by L on 2017/5/4.
 */

public class ChangePasswordActivity extends BaseActivity {


    private static final String TAG = ChangePasswordActivity.class.getSimpleName();
    @Bind(R.id.common_title_left)
    ImageView commonTitleLeft;

    @Bind(R.id.et_oldpassword)
    EditText etOldpassword;
    @Bind(R.id.et_newpassword)
    EditText etNewpassword;
    @Bind(R.id.et_newpassword2)
    EditText etNewpassword2;
    @Bind(R.id.buttonId)
    Button buttonId;
    @Bind(R.id.common_title_title)
    TextView commonTitleTitle;
    String oldpassword, newpassword, newpassword2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changepassword);
        SnakeApplication.changePasswordActivity = this;
        ButterKnife.bind(this);
        this.initView();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void initView() {
        commonTitleLeft.setVisibility(View.VISIBLE);
        commonTitleTitle.setText("修改密码");
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.common_title_left, R.id.buttonId})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.buttonId:
                oldpassword = etOldpassword.getText().toString();
                newpassword = etNewpassword.getText().toString();
                newpassword2 = etNewpassword2.getText().toString();
                if (oldpassword.length() < 6 || newpassword.length() < 6 || newpassword2.length() < 6) {
                    ToastUtils.showShort("请输入6—12位密码");
                } else if (oldpassword.equals(newpassword) && oldpassword.equals(newpassword2)) {
                    ToastUtils.showShort("旧密码和新密码不可相等");
                } else {
                    if (!newpassword.equals(newpassword2)) {
                        ToastUtils.showShort("两次输入的新密码不一致,请重新输入");
                    } else {
                        updateCurrentUserPwd(oldpassword, newpassword);
                    }
                }
                break;
            case R.id.common_title_left:
                finish();
                break;
        }
    }

    private void updateCurrentUserPwd(String oldpassword, String newpassword) {
        addSubscription(BmobUser.updateCurrentUserPassword(oldpassword, newpassword, new UpdateListener() {

            @Override
            public void done(BmobException e) {
                if (e == null) {
                    ToastUtils.showShort("密码修改成功，可以用新密码进行登录");
                    SharedPreferencesUtil.getInstance().clearStringValue("password");
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    Constants.isChangePassword = true;
                    startActivity(intent);
                    BmobUser.logOut();
                    SnakeApplication.mainActivity.getActivity().finish();
                    ChangePasswordActivity.this.finish();
                } else {
                    switch (e.getErrorCode()) {
                        case Constants.OldPassword_Error_Code:
                            ToastUtils.showShort("旧密码输入错误");
                            break;
                        case Constants.Network_Error_Code:
                            ToastUtils.showShort("登录失败,请检查您的网络");
                            break;
                        case Constants.Network_Timeout_Code:
                            ToastUtils.showShort("网络超时");
                            break;
                    }
                    loge(e, TAG);
                }
            }
        }));
    }
}
